package com.wwj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wwj.reggie.common.BaseContext;
import com.wwj.reggie.common.R;
import com.wwj.reggie.entity.ShoppingCart;
import com.wwj.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}", shoppingCart);

        //获得当前用户id
        Long currentId = BaseContext.getCurrentId();
        //获得菜品id
        Long dishId = shoppingCart.getDishId();
        //获得菜品口味
//        String dishFlavor = shoppingCart.getDishFlavor();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);

        //判断添加的是菜品还是套餐
        if (dishId != null){
            lqw.eq(ShoppingCart::getDishId, dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询当前用户添加的菜品或套餐是否已存在
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        if (cartServiceOne != null){
            /*if (cartServiceOne.getDishFlavor().equals(dishFlavor)){ //判断该口味是否存在
                cartServiceOne.setNumber(cartServiceOne.getNumber() + 1); //存在：数量+1
                shoppingCartService.updateById(cartServiceOne);
                shoppingCart = cartServiceOne;
            }else {
                shoppingCart.setUserId(currentId);
                shoppingCartService.save(shoppingCart);//不存在：向数据库中添加数据
            }*/
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1); //存在：数量+1
            shoppingCartService.updateById(cartServiceOne);
            shoppingCart = cartServiceOne;

            return R.success(shoppingCart);
        }

        //不存在：向数据库中添加数据
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(currentId);
        shoppingCartService.save(shoppingCart);

        return R.success(shoppingCart);
    }

    /**
     * 减少商品数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("===> {}", shoppingCart);

        //获得当前用户id
        Long currentId = BaseContext.getCurrentId();
        //获得菜品id
        Long dishId = shoppingCart.getDishId();
        //获得套餐id
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null){
            lqw.eq(ShoppingCart::getDishId, dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId, setmealId);
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        int number = cartServiceOne.getNumber(); //获得该菜品或套餐的份数

        //判断该菜品或套餐的份数是否为最低份数
        if (number > 1) {
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            cartServiceOne.setNumber(0);
            shoppingCartService.removeById(cartServiceOne.getId()); //份数为0自动从数据库中删除
        }


        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");

        //获得当前用户id
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //获得当前用户id
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);

        shoppingCartService.remove(lqw);

        return R.success("");
    }
}
